package org.springblog.springblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblog.springblog.common.pojo.dataobject.BlogInfo;
import org.springblog.springblog.common.pojo.response.BlogInfoResponse;
import org.springblog.springblog.common.utils.BeanConver;
import org.springblog.springblog.mapper.BlogInfoMapper;
import org.springblog.springblog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogMapper;

    /**
     * 获取博客列表
     * @return
     */
    @Override
    public List<BlogInfoResponse> getList() {
        //查询数据库
        List<BlogInfo> blogInfos = blogMapper.selectList(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getDeleteFlag, 0).orderByDesc(BlogInfo::getId));
        //转换
        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream()
                .map(blogInfo -> BeanConver.trans(blogInfo)).collect(Collectors.toList());

        return blogInfoResponses;
    }

    /**
     * 获取博客详情
     * @param blogId
     * @return
     */
    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = selectBlogById(blogId);
        return BeanConver.trans(blogInfo);
    }

    /**
     * 从数据库查询博客详情
     * @param blogId
     * @return
     */
    public BlogInfo selectBlogById(Integer blogId){
        return blogMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId).eq(BlogInfo::getDeleteFlag, 0));
    }
}
